package cn.gov.msa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Apply;
import cn.gov.msa.model.Menu;
import cn.gov.msa.model.Role;
import cn.gov.msa.model.SupervisioRule;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.service.SuperviseService;
import cn.gov.msa.util.GsonHelper;
import cn.gov.msa.util.InfoObjUtil;
import cn.gov.msa.util.ObjectUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/supervise", produces = "text/plain;charset=UTF-8")
public class SuperviseController {
	private ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private SuperviseService superviseService;

	@RequestMapping(value = "/limitList")
	public String limitList(@RequestBody JSONObject params) throws Exception {
		String result = "";
		int pageNum = 1;
		int pageSize = 5;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		if (params.has("startApplyTime") && !params.has("endApplyTime")) {
			params.put("endApplyTime", ObjectUtil.ConvertDataToString(new Date()));
		}

		Map map = mapper.readValue(params.toString(), Map.class);
		List<Map> limitList = superviseService.limitList(pageNum, pageSize, map);
		// 获取分页信息
		PageInfo page = new PageInfo(limitList);
		JSONArray ja = JSONArray.fromObject(limitList);
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		result = jsonResult.toString();
		return result;
	}

	@RequestMapping(value="/complaintByParam")
	public String complaintByParam(@RequestBody JSONObject params,HttpServletRequest request)throws Exception {
		String result = "";
		int pageNum = 1;
		int pageSize = 5;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		UserDetail userDetail = (UserDetail) request.getSession().getAttribute("userDetail");
		params.put("orgCode", userDetail.getOrgcode());

		Map map = mapper.readValue(params.toString(), Map.class);
		List<Map> limitList = superviseService.complaintDetail(pageNum, pageSize, map);
		// 获取分页信息
		PageInfo page = new PageInfo(limitList);
		JSONArray ja = JSONArray.fromObject(limitList);
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		result = jsonResult.toString();
		System.err.println(result);
		return result;
	}
	
	
	@RequestMapping(value = "/complaintList")
	public String complaintList(@RequestBody JSONObject params) throws Exception {

		String result = "";
		int pageNum = 1;
		int pageSize = 5;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		if (params.has("startApplyTime") && !params.has("endApplyTime")) {
			params.put("endApplyTime", ObjectUtil.ConvertDataToString(new Date()));
		}

		Map map = mapper.readValue(params.toString(), Map.class);
		List<Map> limitList = superviseService.complaintList(pageNum, pageSize, map);
		// 获取分页信息
		PageInfo page = new PageInfo(limitList);
		JSONArray ja = JSONArray.fromObject(limitList);
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		result = jsonResult.toString();

		return result;
	}

	@RequestMapping(value = "/ruleList")
	public String ruleList(@RequestBody JSONObject params, HttpServletRequest req) throws Exception {

		String result = "";
		int pageNum = 1;
		int pageSize = 5;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		if (params.has("startApplyTime") && !params.has("endApplyTime")) {
			params.put("endApplyTime", ObjectUtil.ConvertDataToString(new Date()));
		}

		Map rule = mapper.readValue(params.toString(), Map.class);
		List<SupervisioRule> limitList = superviseService.ruleList(pageNum, pageSize, rule);
		// 获取分页信息
		PageInfo page = new PageInfo(limitList);
		JSONArray ja = JSONArray.fromObject(limitList);
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		result = jsonResult.toString();

		return result;
	}

	@RequestMapping(value = "/delete")
	public String delete(String ruleId) throws Exception {
		int result = 0;
		String info = "";

		JSONArray japaram = new JSONArray();

		try {
			JSONObject param = new JSONObject();
			param.put("ruleId", ruleId);
			japaram.add(param);

			int delete = superviseService.delete(ruleId);

			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? "删除成功！" : "删除失败！";
		} catch (Exception e) {
			result = 2;
			info = "删除异常!";
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = "";
		boolean save = false;

		try {

			// 得到类型的model数据
			SupervisioRule rule = mapper.readValue(param.toString(), SupervisioRule.class);

			save = superviseService.update(rule);
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		JSONObject jsonResult = new JSONObject();
		if (save) {
			result = 1;
			info = InfoObjUtil.Success_save_OK;
		} else {
			result = 0;
			info = InfoObjUtil.Error_save_no;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/getInfo")
	public String getInfo(String ruleId) throws Exception {
		int result = 0;
		String info = "";

		JSONObject joparam = new JSONObject();

		try {

			SupervisioRule rule = superviseService.selectByRuleId(ruleId);

			result = rule != null ? 1 : 0;
			info = rule != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			joparam = JSONObject.fromObject(rule);
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		return jsonResult.toString();
	}
	
	@RequestMapping(value="/save")
	public String saveData(@RequestBody JSONObject param)throws Exception{
		int result = 0;
		String info = "";
		boolean save = false;
		
		// 新增前,查询是否有重复的规则
		Map<String,String> map = new HashMap<String,String>();
		map.put("itemNo", param.getString("itemno"));
		map.put("remindRuleType", param.getString("remindruletype"));
		
		List<SupervisioRule> ruleList = superviseService.ruleList(1, 1, map);
		// 没有重复的规则
		if(ruleList == null || ruleList.size()==0){
			// 得到类型的model数据
			try {
				SupervisioRule rule = mapper.readValue(param.toString(), SupervisioRule.class);
				save = superviseService.save(rule);
			} catch (Exception e) {
				result = 2;
				info = "数据保存异常！";
			}
			
			if (save) {
				result = 1;
				info = "数据保存成功！";
			} else {
				result = 0;
				info = "数据保存失败！";
			}
		} else {
			result = 0;
			info = "您新增的监督规则已存在！";
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
}
